package exam.web.action;

import java.util.Date;
import java.util.List;

import exam.common.util.KeyCommon;
import exam.common.util.NullCheckUtil;
import exam.model.dao.SubQuestionDAO;
import exam.model.entity.TExaminationEntity;
import exam.model.entity.TSubQuestionEntity;
import exam.model.entity.TUserEntity;
import exam.web.action.base.ExaminationActionBase;

public class EditExaminationAction extends ExaminationActionBase {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<TSubQuestionEntity> tSubQuestionEntityList;
	
	private SubQuestionDAO subQuestionDAO = new SubQuestionDAO();
	
	private boolean statusAction;
	
	public String execute() throws Exception {
		String btSave = request.getParameter("btSave");
		tSubQuestionEntityList = subQuestionDAO.selectListSubQuestion();
		if (NullCheckUtil.isNUll(btSave)) {
			String examId = request.getParameter(KeyCommon.EXAM_ID);
			if (NullCheckUtil.isNUll(examId)) {
				return ERROR;
			}
			settExaminationEntity(examDAO.selectByPk(Long.valueOf(examId)));
			return INPUT;
		} else {
			TExaminationEntity tExamination = examDAO.selectByPk(gettExaminationEntity().getExamId());
			if (NullCheckUtil.isNotNull(tExamination)) {
				tExamination.setUpdateDate(new Date());
				tExamination.setExamTitle(gettExaminationEntity().getExamTitle());
				tExamination.setExamTime(gettExaminationEntity().getExamTime());
				tExamination.setQuestNum(gettExaminationEntity().getQuestNum());
				tExamination.setStatus(isStatusAction()?1:0);
				tExamination.setSubQuestionEntity(
						new TSubQuestionEntity(gettExaminationEntity().getSubQuestId()));
				tExamination.setUserEntity(
						new TUserEntity(gettExaminationEntity().getUserId()));
				examDAO.updateExamination(tExamination);
				addActionMessage(getText("Cập nhật thông tin đề thi thành công"));
			} else {
				addActionError(getText("Cập nhật thông tin đề thi thất bại"));
			}
			return SUCCESS;
		}
			
	}
	
	public List<TSubQuestionEntity> gettSubQuestionEntityList() {
		return tSubQuestionEntityList;
	}

	public void settSubQuestionEntityList(List<TSubQuestionEntity> tSubQuestionEntityList) {
		this.tSubQuestionEntityList = tSubQuestionEntityList;
	}

	public boolean isStatusAction() {
		return statusAction;
	}

	public void setStatusAction(boolean statusAction) {
		this.statusAction = statusAction;
	}
}
